//
//  MapViewController.m
//  超优易购
//
//  Created by 超优易购 on 16/1/13.
//  Copyright © 2016年 超优易购. All rights reserved.
//

#import "MapViewController.h"

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件

#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件

#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件

#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
#import "MyAnimatedAnnotationView.h"
@interface MapViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,UITableViewDataSource,UITableViewDelegate,BMKGeoCodeSearchDelegate,BMKPoiSearchDelegate,UISearchBarDelegate>

@property (nonatomic) BOOL showMapScaleBar;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *MyView;

@property (nonatomic, strong) NSMutableArray *nameArray;

@end

@implementation MapViewController{
    
    BMKMapView* _mapView;
 
    BMKLocationService* _locService;
    //以下是大头钉
    BMKPointAnnotation* pointAnnotation;
    
    BMKPointAnnotation* animatedAnnotation;
    
    BMKPinAnnotationView *annotationView;
    
    BMKPinAnnotationView *newAnnotation;
    
    //以下是编码和反编码
    
    BMKGeoCodeSearch* _geocodesearch;
    
    bool isGeoSearch;
    
    CLLocationCoordinate2D temp;
    
    //以下是POI搜索
    
    BMKPoiSearch* _poisearch;
    
    //以下是searchbar代码
    
    UISearchBar *searchBar;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2)];
    
    _locService = [[BMKLocationService alloc]init];
    
    _geocodesearch = [[BMKGeoCodeSearch alloc]init];
    
    _poisearch = [[BMKPoiSearch alloc]init]; 


    [self.view addSubview:_mapView];

    self.showMapScaleBar = YES;
   
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.tableView];
    
    self.MyView = [[UIView alloc] init];
    self.MyView .frame = CGRectMake(0, 0, 20, 20);
    self.MyView .backgroundColor = [UIColor yellowColor];
    self.MyView .center = _mapView.center;
    
    [_mapView addSubview:self.MyView];
    
    temp = {0,0};
    //以下是searchbar代码
    
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    
    [searchBar setPlaceholder:@"请在此输入你想要查找的地点"];
    
    [searchBar setShowsCancelButton:YES];
    
    searchBar.delegate = self;
    
    [searchBar sizeToFit];
    
    self.navigationItem.titleView = searchBar;
    
    
    
}
-(void)viewWillAppear:(BOOL)animated {
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    _locService.delegate = self;
    _geocodesearch.delegate = self;
    _poisearch.delegate = self;
    
    [_locService startUserLocationService];
    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;
    
    [_mapView setZoomLevel:15];
    
    annotationView.center = self.view.center;


}

-(void)viewWillDisappear:(BOOL)animated {
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    _locService.delegate = nil;
    _geocodesearch.delegate = nil;
    _poisearch.delegate = nil;
}
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
        NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    [_mapView updateLocationData:userLocation];
    [_locService stopUserLocationService];

    
  
}


/**
 *地图区域即将改变时会调用此接口
 *@param mapview 地图View
 *@param animated 是否动画
 */
//以下是反向地理编码代码
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
   
    if (error == 0) {

        _mapView.centerCoordinate = result.location;
        NSString* titleStr;
        NSString* showmeg;
        titleStr = @"反向地理编码";
        showmeg = [NSString stringWithFormat:@"%@",result.address];
        
        NSLog(@"%@",showmeg);
  
    }
}




  


- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    
    if ((temp.latitude != mapView.centerCoordinate.latitude )&&(temp.longitude!= mapView.centerCoordinate.longitude )) {
        
        self.MyView.center = _mapView.center;
        
        isGeoSearch = false;
        
        CLLocationCoordinate2D pt = mapView.centerCoordinate;
        
        
        BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
        
        reverseGeocodeSearchOption.reverseGeoPoint = pt;

        [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
        
        temp = mapView.centerCoordinate;
        
        //以下是poisearch代码
        BMKNearbySearchOption *nearbySearchOption = [[BMKNearbySearchOption alloc]init];
        
        nearbySearchOption.keyword = @"小区";
   
        nearbySearchOption.location = mapView.centerCoordinate;
        
        nearbySearchOption.radius = 300;
        
        [_poisearch poiSearchNearBy:nearbySearchOption];
     
        
    }
    

}
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{

    self.nameArray = [NSMutableArray array];

    
    if (error == BMK_SEARCH_NO_ERROR) {
        for (int i = 0; i < result.poiInfoList.count; i++) {
            BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
         
            
            [self.nameArray addObject:poi];
            
            [self.tableView reloadData];
            
        }
    } else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
        NSLog(@"起始点有歧义");
    } else {
        // 各种情况的判断。。。
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.nameArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height/(2 * 10);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.textLabel.text = [self.nameArray[indexPath.row] name];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _mapView.centerCoordinate = [self.nameArray[indexPath.row] pt];
}
//以下代码是searchbar的
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    searchBar.text = nil;
    
    [searchBar resignFirstResponder];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
  
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
     [self becomeFirstResponder];
    
    BMKNearbySearchOption *nearbySearchOption = [[BMKNearbySearchOption alloc]init];
    
    nearbySearchOption.keyword = @"小区";
    
    nearbySearchOption.location = _mapView.centerCoordinate;
    
    nearbySearchOption.radius = 300;
    
    [_poisearch poiSearchNearBy:nearbySearchOption];

   
    return YES;
    
}
@end
